package com.dd.cloud.common.annotate.validation.impl;

import com.dd.cloud.common.annotate.validation.Business;
import com.dd.cloud.common.constants.RegularConstants;
import com.dd.cloud.common.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 营业时间、有效期验证实现类
 *
 * @author zk
 */
public class BusinessValidation implements ConstraintValidator<Business, Object> {
    private Business business;

    @Override
    public void initialize(Business businessVal) {
        business = businessVal;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        String ph = (String) value;
        if (!business.required() && StringUtils.isBlank(ph)) {
            return true;
        }
        if (business.required() && StringUtils.isBlank(ph)) {
            return false;
        }
        return RegexUtils.regCheck(ph, RegularConstants.BUSINESS_REGEXP);
    }
}
